package com.eboss2.sbs.activity;

import android.content.Context;
import android.graphics.Color;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.support.v4.view.ViewPager;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.eboss2.sbs.R;
import com.eboss2.sbs.adapter.FragmentAdapter;
import com.eboss2.sbs.fragment.FragmentOrderManagerAll;
import com.eboss2.sbs.fragment.FragmentOrderManagerDfh;
import com.eboss2.sbs.fragment.FragmentOrderManagerDfk;
import com.eboss2.sbs.fragment.FragmentOrderManagerTkjj;
import com.eboss2.sbs.fragment.FragmentOrderManagerTkz;
import com.eboss2.sbs.fragment.FragmentOrderManagerYfh;
import com.eboss2.sbs.fragment.FragmentOrderManagerYgb;
import com.eboss2.sbs.fragment.FragmentOrderManagerYqx;
import com.eboss2.sbs.fragment.FragmentOrderManagerYtk;
import com.eboss2.sbs.fragment.FragmentOrderManagerYwc;
import com.mytools.tool.activityManager.MyActivityManager;
import com.tool.utils.utils.StatusBarUtil;

import net.lucode.hackware.magicindicator.MagicIndicator;
import net.lucode.hackware.magicindicator.ViewPagerHelper;
import net.lucode.hackware.magicindicator.buildins.commonnavigator.CommonNavigator;
import net.lucode.hackware.magicindicator.buildins.commonnavigator.abs.CommonNavigatorAdapter;
import net.lucode.hackware.magicindicator.buildins.commonnavigator.abs.IPagerIndicator;
import net.lucode.hackware.magicindicator.buildins.commonnavigator.abs.IPagerTitleView;
import net.lucode.hackware.magicindicator.buildins.commonnavigator.indicators.LinePagerIndicator;
import net.lucode.hackware.magicindicator.buildins.commonnavigator.titles.ColorTransitionPagerTitleView;
import net.lucode.hackware.magicindicator.buildins.commonnavigator.titles.SimplePagerTitleView;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

public class MyOrderManagerActivity extends FragmentActivity implements View.OnClickListener{

	private static final String[] CHANNELS = new String[]{"全部", "待付款", "待发货", "已发货",  "退款中", "已完成", "已退款","已关闭","已取消", "退款拒绝"};//"待评价",
	private List<String> mDataList = Arrays.asList(CHANNELS);
	private List<Fragment> mFragmentList = new ArrayList<Fragment>();
	private FragmentAdapter mFragmentAdapter;
	//ViewPager的当前选中页
	private int currentIndex;
	private Context mContext;

	private ViewPager mViewPager;


	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);

		mContext = this;
		setContentView(R.layout.activity_order_manager);
		MyActivityManager.getInstance().addActivity(this);
//		StatusBarUtil.setColor(this, getResources().getColor(R.color.statusBar), 0);
		initView();
	}

	private void initView(){
		RelativeLayout relativeLayout = findViewById(R.id.lv_heard);
		relativeLayout.setBackgroundColor(getResources().getColor(R.color.statusBar));
		showView(R.id.titleback_btn_back, true);
		setTvText(R.id.tv_title, "订单管理");
		findViewById(R.id.titleback_btn_back).setOnClickListener(this);
		mViewPager = (ViewPager) findViewById(R.id.view_pager);

		initMagicIndicator();

		//将三个页面添加到容器里面
		mFragmentList.add(new FragmentOrderManagerAll());
		mFragmentList.add(new FragmentOrderManagerDfk());
		mFragmentList.add(new FragmentOrderManagerDfh());
		mFragmentList.add(new FragmentOrderManagerYfh());
//		mFragmentList.add(new FragmentOrderManagerDpj());
		mFragmentList.add(new FragmentOrderManagerTkz());
		mFragmentList.add(new FragmentOrderManagerYwc());
		mFragmentList.add(new FragmentOrderManagerYtk());
		mFragmentList.add(new FragmentOrderManagerYgb());
		mFragmentList.add(new FragmentOrderManagerYqx());
		mFragmentList.add(new FragmentOrderManagerTkjj());

		//重写一个FragmentAdapter继承FragmentPagerAdapter，需要传FragmentManager和存放页面的容器过去
		mFragmentAdapter = new FragmentAdapter(getSupportFragmentManager(), mFragmentList);
		//ViewPager绑定监听器
		mViewPager.setAdapter(mFragmentAdapter);
		//ViewPager设置默认当前的项
		mViewPager.setCurrentItem(0);
		mViewPager.setOffscreenPageLimit(6);
		//ViewPager设置监听器，需要重写onPageScrollStateChanged，onPageScrolled，onPageSelected三个方法
		mViewPager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {

			/**
			 * state滑动中的状态 有三种状态（0，1，2） 1：正在滑动 2：滑动完毕 0：什么都没做。
			 * 三个方法的执行顺序为：用手指拖动翻页时，最先执行一遍onPageScrollStateChanged（1），
			 * 然后不断执行onPageScrolled，放手指的时候，直接立即执行一次onPageScrollStateChanged（2），
			 * 然后立即执行一次onPageSelected，然后再不断执行onPageScrolled，
			 * 最后执行一次onPageScrollStateChanged（0）。
			 */

			/**
			 * state滑动中的状态 有三种状态（0，1，2） 1：正在滑动 2：滑动完毕 0：什么都没做。
			 */
			@Override
			public void onPageScrollStateChanged(int state) {
				Log.i("PageScroll：", "onPageScrollStateChanged" + ":" + state);
			}

			/**
			 * position :当前页面，及你点击滑动的页面 offset:当前页面偏移的百分比
			 * offsetPixels:当前页面偏移的像素位置
			 */
			@Override
			public void onPageScrolled(int position, float offset,
									   int offsetPixels) {
			}

			/**
			 * 将当前选择的页面的标题设置字体颜色为蓝色
			 */
			@Override
			public void onPageSelected(int position) {
				Log.i("PageScroll：", "onPageSelected" + ":" + position);
				currentIndex = position;
			}
		});

	}


	private void showView(int vid, boolean isShow){
		View view = (View) findViewById(vid);
		if(view!=null){
			view.setVisibility(isShow?View.VISIBLE:View.GONE);
		}
	}


	private void setTvText(int vid, String text) {
		TextView view = (TextView) findViewById(vid);
		if(view!=null){view.setText(TextUtils.isEmpty(text) ? "": text);}
	}



	private void initMagicIndicator() {
		MagicIndicator magicIndicator = (MagicIndicator) findViewById(R.id.magic_indicator);
		magicIndicator.setBackgroundColor(Color.WHITE);
		CommonNavigator commonNavigator = new CommonNavigator(this);
		commonNavigator.setAdapter(new CommonNavigatorAdapter() {
			@Override
			public int getCount() {
				return mDataList == null ? 0 : mDataList.size();
			}

			@Override
			public IPagerTitleView getTitleView(Context context, final int index) {
				SimplePagerTitleView simplePagerTitleView = new ColorTransitionPagerTitleView(context);
				simplePagerTitleView.setText(mDataList.get(index));
				simplePagerTitleView.setNormalColor(Color.parseColor("#333333"));
				simplePagerTitleView.setSelectedColor(Color.parseColor("#0092FF"));
				simplePagerTitleView.setOnClickListener(new View.OnClickListener() {
					@Override
					public void onClick(View v) {
						mViewPager.setCurrentItem(index);
					}
				});
				return simplePagerTitleView;
			}

			@Override
			public IPagerIndicator getIndicator(Context context) {
				LinePagerIndicator indicator = new LinePagerIndicator(context);
				indicator.setColors(Color.parseColor("#0980d8"));
				return indicator;
			}
		});
		magicIndicator.setNavigator(commonNavigator);
		ViewPagerHelper.bind(magicIndicator, mViewPager);
	}



	@Override
	public void onClick(View v) {
		switch (v.getId()) {
			case R.id.titleback_btn_back:
				finish();
				break;

			default: break;
		}
	}
	

}
